<template>
	<view>

		<u-popup :show="showQrcode" mode="center" @close="closeQrcode" :round="10" :safeAreaInsetBottom="false">
			<view class="share_pic">
				<image :src="shareQr" mode="widthFix"></image>
			</view>
			<view class="share_button">
				<view class="share_box" @click="shareWx">
					<image :src="domain + '/icons/wxico.png'" mode=""></image>
					<text>发送给好友</text>
				</view>
				<view class="share_box" @click="savePoster">
					<image :src="domain + '/icons/photo.png'" mode=""></image>
					<text>保存到相册</text>
				</view>
			</view>
		</u-popup>

	</view>
</template>

<script>
	
	
	export default {
		data() {
			return {
				showQrcode: false,
				domain : uni.getStorageSync('domain'),
				shareQr : '',
				city : ''
			}
		},
		props : {
			topicCurrentId : '',
			currentSwiperIndex : ''
		},
		methods: {
			getQrcode() {
				// console.log('uni.getStorageSync(\'domain\')', uni.getStorageSync('domain'))
				switch(uni.getStorageSync('domain')){
					case 'https://aikm.cn' :
					// case 'http://aikm.cc' :
					this.city = 'km'
					break
					
					case 'https://ks.aikm.cn' :
					this.city = 'ks'
					break
					
					default :					
					this.city = 'km'
					break
				}
				uni.showLoading({
					title:'加载中...'
				})
				uni.request({
					url : uni.getStorageSync('domain') + '/api/v1/share/wx_miniapp_code/' + this.topicCurrentId,
					method : "GET",
					data: {
						currentSwiperIndex : this.currentSwiperIndex,
						city : this.city
					},
					success : (res) => {
						uni.hideLoading()
						this.shareQr = uni.getStorageSync('domain') + res.data.share_qr;
						uni.setStorageSync('qrcodeurl', this.shareQr);
					}
				})
				this.showQrcode = true
			},
			closeQrcode() {
				this.showQrcode = false
			},
			shareWx: (e) => {
				uni.downloadFile({
					url: uni.getStorageSync('qrcodeurl'),
					success: (res) => {
						uni.showShareImageMenu({
							path: res.tempFilePath,
							success: (e) => {
								uni.hideLoading();
								uni.showToast({
									title:'分享成功',
									icon:'success'
								})
							},
							fail: (e) => {
								uni.hideLoading();
							}
						})
					}
				})
			},
			savePoster: (e) => {
				uni.downloadFile({
					url: uni.getStorageSync('qrcodeurl'),
					success: (res) => {
						uni.saveImageToPhotosAlbum({
							filePath: res.tempFilePath,
							success: (res) => {
								uni.hideLoading();
								uni.showToast({
									title:'保存成功',
									icon:'success'
								})
							},
							fail: (e) => {
								uni.hideLoading();
							}
						})
					}
				})
			},

		}
	}
</script>

<style>

</style>
